package com.nkym.wypan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nkym.wypan.exception.BusinessException;
import com.nkym.wypan.model.context.chat.RoomMemberContext;
import com.nkym.wypan.model.entity.UserInfo;
import com.nkym.wypan.model.entity.chat.Room;
import com.nkym.wypan.model.entity.chat.RoomMember;
import com.nkym.wypan.model.resp.socket.ChatUserInfoVO;
import com.nkym.wypan.service.RoomMemberService;
import com.nkym.wypan.mapper.RoomMemberMapper;
import com.nkym.wypan.service.RoomService;
import com.nkym.wypan.service.UserInfoService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.nkym.wypan.constants.DeleteStatusConstant.*;
import static com.nkym.wypan.enums.ResponseStatus.*;
import static com.nkym.wypan.enums.chat.RoomMemberRoleEnum.*;

/**
 * @author 19637
 * @description 针对表【wy_pan_room_member(群成员表)】的数据库操作Service实现
 * @createDate 2023-11-11 11:17:00
 */
@Service
public class RoomMemberServiceImpl extends ServiceImpl<RoomMemberMapper, RoomMember>
        implements RoomMemberService {
    @Resource
    RoomMemberMapper roomMemberMapper;
    @Resource
    @Lazy
    RoomService roomService;
    @Resource
    UserInfoService userInfoService;

    @Override
    public List<ChatUserInfoVO> getMember(RoomMemberContext roomMemberContext) {
        Long roomId = roomMemberContext.getRoomId();
        Long userId = roomMemberContext.getUserId();

        boolean isExists = roomService.checkRoomIsExists(roomId);
        boolean isRoomMember = checkUserIsRoomMember(roomId, userId);

        if (!isExists) {
            throw new BusinessException(ROOM_NOT_EXISTS);
        }
        if (!isRoomMember) {
            throw new BusinessException(NOT_ROOM_MEMBER);
        }

        List<UserInfo> roomUserInfoList = doGetMember(roomId, userId);
        return roomUserInfoList.stream().map(ChatUserInfoVO::covert).collect(Collectors.toList());
    }

    @Override
    public boolean checkUserIsRoomMember(Long roomId, Long userId) {
        RoomMember roomMember = roomMemberMapper.selectOne(new LambdaQueryWrapper<RoomMember>().eq(RoomMember::getUid, userId)
                .eq(RoomMember::getRoomId, roomId)
                .ne(RoomMember::getRole, KICKED_OUT.getCode()));
        return roomMember != null;
    }

    /**
     * ---------------------------private-------------------------------------
     */


    private List<UserInfo> doGetMember(Long roomId, Long userId) {
        List<RoomMember> roomMemberIds = roomMemberMapper.selectList(new LambdaQueryWrapper<RoomMember>()
                .select(RoomMember::getUid)
                .eq(RoomMember::getRoomId, roomId)
                .ne(RoomMember::getRole, KICKED_OUT.getCode()));
        List<Long> userIds = roomMemberIds.stream().map(RoomMember::getUid).collect(Collectors.toList());
        return userInfoService.listByIds(userIds);
    }
}




